package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.WebNavigation;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.model.info.WebNavigationInfo;
import ysq.xyg.service.WebNavigationService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Yu Shengqiang on 2017/5/3.
 */
@Controller
@RequestMapping("/webNavigation")
public class WebNavigationController extends BaseController{
    private static Logger log= LoggerFactory.getLogger(WebNavigationController.class);
    @Resource
    private WebNavigationService webNavigationService;
    private WebNavigation webNavigation;

    @RequestMapping("/listPage")
    public ModelAndView listPage(HttpServletRequest request, HttpServletResponse response,String useType){
        return new ModelAndView("/webNavigationList").addObject("useType",useType);
    }


    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody
    JSONObject JsonList(WebNavigation webNavigation, Page page) throws UnsupportedEncodingException {
        PageInfo<WebNavigation> pageInfo=webNavigationService.page(webNavigation,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            List<WebNavigation> webNavigationsList=pageInfo.getList();
            List<WebNavigation> webNavigationsLists=new ArrayList<WebNavigation>();
            if(webNavigationsList!=null && webNavigationsList.size() > 0){
                for(WebNavigation webNavigation1:webNavigationsList){
                    if(BaseConstant.MENU_ONE.equals(webNavigation1.getGrade())){
                        webNavigationsLists.add(webNavigation1);
                        for(WebNavigation webNavigation2:webNavigationsList){
                            if(webNavigation1.getId().equals(webNavigation2.getParentId())){
                                webNavigationsLists.add(webNavigation2);
                            }
                        }
                    }
                }
            }
            jsonObject.put("data",webNavigationsLists);
        }
        return jsonObject;
    }
    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(String id,String parentId,String useType){
        ModelAndView view=new ModelAndView("/webNavigationUpdateOrSave");
        WebNavigation webNavigation=null;
        if (id != null && !"".equals(id)){
            webNavigation=webNavigationService.selectByPrimaryKey(id);
            view.addObject(webNavigation);
        }
        if(StringUtil.isNotEmpty(parentId)){
            WebNavigation parentWebNavigation=webNavigationService.selectByPrimaryKey(parentId);
            if(webNavigation==null){
                webNavigation=new WebNavigation();
            }
            webNavigation.setParentId(parentId);
            webNavigation.setParentName(parentWebNavigation.getName());
            view.addObject(webNavigation);
        }
        view.addObject("useType",useType);
        return view;
    }
    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject UpdateOrAddEntity(WebNavigationInfo webNavigationInfo){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(webNavigationInfo.getId())){
            webNavigation=new WebNavigation();
            webNavigation.setId(BaseUtil.uId());
            webNavigation.setCreateTime(new Date());
            webNavigation.setPicture(webNavigationInfo.getPicture());
            webNavigation.setName(webNavigationInfo.getName());
            webNavigation.setUseType(webNavigationInfo.getUseType());
            webNavigation.setUrl(webNavigationInfo.getUrl());
            webNavigation.setSort(webNavigationInfo.getSort());
            webNavigation.setParentId(webNavigationInfo.getParentId());
            webNavigation.setType(webNavigationInfo.getType());
            if(StringUtil.isNotEmpty(webNavigation.getParentId())){
                WebNavigation parentWebNavigation=webNavigationService.selectByPrimaryKey(webNavigationInfo.getParentId());
                //如果存在parentWebNavigation已存在子菜单
                WebNavigation record=new WebNavigation();
                record.setParentId(parentWebNavigation.getId());
                int info=webNavigationService.selectCount(record);
                if (info!=0){
                    webNavigation.setCode(webNavigationService.AcceptMaxMenuCode(webNavigationInfo.getParentId(),Integer.parseInt(parentWebNavigation.getGrade())+1+""));
                }else{
                    webNavigation.setCode(parentWebNavigation.getCode()+webNavigationService.AcceptMaxMenuCode(webNavigationInfo.getParentId(),Integer.parseInt(parentWebNavigation.getGrade())+1+""));
                }
                webNavigation.setGrade(Integer.parseInt(parentWebNavigation.getGrade())+1+"");
            }else{
                webNavigation.setCode(webNavigationService.AcceptMaxMenuCode(null));//menu_one是一级菜单
                webNavigation.setGrade(BaseConstant.MENU_ONE);
            }
            int id=webNavigationService.insert(webNavigation);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL,BaseConstant.DATABASE_ERROR_INSERT);
            }
        }else{
            webNavigation=webNavigationService.selectByPrimaryKey(webNavigationInfo.getId());
            if(StringUtil.isEmpty(webNavigation.getCode())){
                if(webNavigation.getGrade()==BaseConstant.MENU_ONE){
                    webNavigation.setCode(webNavigationService.AcceptMaxMenuCode(webNavigation.getParentId(),webNavigation.getGrade()));
                }else{
                    WebNavigation parentWebNavigation=webNavigationService.selectByPrimaryKey(webNavigationInfo.getParentId());
                    webNavigation.setCode(webNavigationService.AcceptMaxMenuCode(webNavigation.getParentId(),webNavigation.getGrade()));
                }
            }
            webNavigation.setPicture(webNavigationInfo.getPicture());
            webNavigation.setName(webNavigationInfo.getName());
            webNavigation.setUrl(webNavigationInfo.getUrl());
            webNavigation.setSort(webNavigationInfo.getSort());
            webNavigation.setType(webNavigationInfo.getType().replace("\"","'"));
            webNavigation.setCreateTime(new Date());
            int id=webNavigationService.updateByPrimaryKey(webNavigation);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL,BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject DeleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL,BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            List<WebNavigation> list=webNavigationService.list(new WebNavigation(id));
            if(list!=null&&list.size()>0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL,"请删除该导航下子导航！");
            }else{
                int ids=webNavigationService.deleteByPrimaryKey(id);
                if(ids <= 0){
                    returnInfo=new ReturnInfo(BaseConstant.FAIL,BaseConstant.DATABASE_ERROR_DELETE);
                }
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }


}
